package com.mdd.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mdd.product.entity.ProductPropertyValue;
import com.mdd.product.mapper.ProductPropertyValueMapper;
import com.mdd.product.service.IProductPropertyValueService;
import com.mdd.product.validate.ProductPropertyValueCreateValidate;
import com.mdd.product.validate.ProductPropertyValueUpdateValidate;
import com.mdd.product.vo.ProductPropertyValueDetailVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

/**
 * 商品属性值实现类
 * @author LikeAdmin
 */
@Service
public class ProductPropertyValueServiceImpl implements IProductPropertyValueService {

    @Resource
    ProductPropertyValueMapper productPropertyValueMapper;

    /**
     * 商品属性值列表
     *
     * @author LikeAdmin
     * @param propertyId 属性id
     * @return PageResult<OmsProductPropertyValueListedVo>
     */
    @Override
    public List<ProductPropertyValueDetailVo> list(Long propertyId) {
        LambdaQueryWrapper<ProductPropertyValue> propertyWrapper = new LambdaQueryWrapper<>();
        propertyWrapper.eq(ProductPropertyValue::getPropertyId,propertyId);
        List<ProductPropertyValue> data = productPropertyValueMapper.selectList(propertyWrapper);

        List<ProductPropertyValueDetailVo> list = new LinkedList<>();
        for(ProductPropertyValue item : data) {
            ProductPropertyValueDetailVo vo = new ProductPropertyValueDetailVo();
            BeanUtils.copyProperties(item, vo);
            list.add(vo);
        }

        return list;
    }

    /**
     * 商品属性值新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Transactional
    @Override
    public void add(ProductPropertyValueCreateValidate createValidate) {
        ProductPropertyValue model = new ProductPropertyValue();
        model.setPropertyId(createValidate.getPropertyId());
        model.setName(createValidate.getName());
        model.setStatus(0);
        model.setRemark(createValidate.getRemark());
        productPropertyValueMapper.insert(model);
    }

    /**
     * 商品属性值编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Transactional
    @Override
    public void edit(ProductPropertyValueUpdateValidate updateValidate) {
        ProductPropertyValue model = productPropertyValueMapper.selectById(updateValidate.getId());

        Assert.notNull(model, "商品属性值数据不存在!");

        model.setId(updateValidate.getId());
        model.setPropertyId(updateValidate.getPropertyId());
        model.setName(updateValidate.getName());
        model.setStatus(0);
        model.setRemark(updateValidate.getRemark());
        productPropertyValueMapper.updateById(model);
    }

    /**
     * 商品属性值删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Transactional
    @Override
    public void del(Long id) {
        ProductPropertyValue model = productPropertyValueMapper.selectById(id);

        Assert.notNull(model, "数据不存在!");

        productPropertyValueMapper.deleteById(id);
    }

    @Override
    public void delByPropertyIds(List propertyIds) {
        LambdaQueryWrapper<ProductPropertyValue> propertyValueLambdaQueryWrapper=new LambdaQueryWrapper<>();
        propertyValueLambdaQueryWrapper.in(ProductPropertyValue::getPropertyId,propertyIds);
        productPropertyValueMapper.delete(propertyValueLambdaQueryWrapper);
    }

    @Override
    public void delByPropertyIdAndNotInValues(Long propertyId, Set<Long> values) {
        LambdaQueryWrapper<ProductPropertyValue> propertyValueLambdaQueryWrapper=new LambdaQueryWrapper<>();
        propertyValueLambdaQueryWrapper.eq(ProductPropertyValue::getPropertyId,propertyId);
        if(!values.isEmpty()) {
            propertyValueLambdaQueryWrapper.notIn(ProductPropertyValue::getId, values);
        }
        productPropertyValueMapper.delete(propertyValueLambdaQueryWrapper);
    }

}
